package eu.gline.km.activities;

import eu.gline.km.Core;
import eu.gline.km.R;
import eu.gline.km.clients.KmClient;
import eu.gline.km.jni.enums.bVk;
import eu.gline.km.jni.enums.dwEvents;
import eu.gline.km.jni.enums.dwFlags;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;

public class MultimediaKeyboard extends Activity implements OnClickListener, OnTouchListener {

	private Core core;
	private KmClient kmc;
	
	private float oldX;
	private float oldY;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.multimediakeyboard);
		core = (Core)getApplication();
		core.connectTo("test", "192.168.212.31", 9900, null);
		kmc = core.getClient("test");
		
		findViewById(R.id.play).setOnClickListener(this);
		findViewById(R.id.next).setOnClickListener(this);
		findViewById(R.id.previous).setOnClickListener(this);
		findViewById(R.id.stop).setOnClickListener(this);
		findViewById(R.id.volp).setOnClickListener(this);
		findViewById(R.id.volm).setOnClickListener(this);
		findViewById(R.id.vold).setOnClickListener(this);
		findViewById(R.id.view).setOnTouchListener(this);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.play:
			kmc.getKeyboard().keyboardEvent(bVk.VK_MEDIA_PLAY_PAUSE, dwFlags.KEYEVENTF_KEYDOWN);
			kmc.getKeyboard().keyboardEvent(bVk.VK_MEDIA_PLAY_PAUSE, dwFlags.KEYEVENTF_KEYUP);
			break;
		case R.id.next:
			kmc.getKeyboard().keyboardEvent(bVk.VK_MEDIA_NEXT_TRACK, dwFlags.KEYEVENTF_KEYDOWN);
			kmc.getKeyboard().keyboardEvent(bVk.VK_MEDIA_NEXT_TRACK, dwFlags.KEYEVENTF_KEYUP);
			break;
		case R.id.previous:
			kmc.getKeyboard().keyboardEvent(bVk.VK_MEDIA_PREV_TRACK, dwFlags.KEYEVENTF_KEYDOWN);
			kmc.getKeyboard().keyboardEvent(bVk.VK_MEDIA_PREV_TRACK, dwFlags.KEYEVENTF_KEYUP);
			break;
		case R.id.stop:
			kmc.getKeyboard().keyboardEvent(bVk.VK_MEDIA_STOP, dwFlags.KEYEVENTF_KEYDOWN);
			kmc.getKeyboard().keyboardEvent(bVk.VK_MEDIA_STOP, dwFlags.KEYEVENTF_KEYUP);
			break;
		case R.id.volp:
			kmc.getKeyboard().keyboardEvent(bVk.VK_VOLUME_UP, dwFlags.KEYEVENTF_KEYDOWN);
			kmc.getKeyboard().keyboardEvent(bVk.VK_VOLUME_UP, dwFlags.KEYEVENTF_KEYUP);
			break;
		case R.id.volm:
			kmc.getKeyboard().keyboardEvent(bVk.VK_VOLUME_MUTE, dwFlags.KEYEVENTF_KEYDOWN);
			kmc.getKeyboard().keyboardEvent(bVk.VK_VOLUME_MUTE, dwFlags.KEYEVENTF_KEYUP);
			break;
		case R.id.vold:
			kmc.getKeyboard().keyboardEvent(bVk.VK_VOLUME_DOWN, dwFlags.KEYEVENTF_KEYDOWN);
			kmc.getKeyboard().keyboardEvent(bVk.VK_VOLUME_DOWN, dwFlags.KEYEVENTF_KEYUP);
			break;
		}
		
	}

	public boolean onTouch(View v, MotionEvent event) {
		//kmc.getMouse().mousePos(Math.round(event.getX()), Math.round(event.getY()));
		//if(event.getAction() == MotionEvent.ACTION_MOVE) {
			if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
				  oldX = event.getX();
				  oldY = event.getY();
			  } else if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
				  int deltaX = Math.round(oldX - event.getX());
				  int deltaY = Math.round(oldY - event.getY());
				  
				  if (event.getY() > oldY) kmc.getMouse().mouseDown(deltaY);
				  else kmc.getMouse().mouseUp(-deltaY);
				  if (event.getX() < oldX) kmc.getMouse().mouseLeft(deltaX);
				  else kmc.getMouse().mouseRight(-deltaX);
				  oldX = event.getX();
				  oldY = event.getY();
			  }/* else if (event.getActionMasked() == MotionEvent.ACTION_UP) {
				  oldX = 0;
				  oldY = 0;
			  }*/
			  //determine which direction finger moved
		//}
		return true;
	}

}
